﻿using Microsoft.WindowsAzure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace WebJobAsTimerService
{
    public class WebJobConfiguration : IConfiguration
    {
        public WebJobConfiguration()
        {
            ExchangeUserName = CloudConfigurationManager.GetSetting("ExchangeUserName");
            ExchangeUserPassword = CloudConfigurationManager.GetSetting("ExchangeUserPassword");
            FromAddress = CloudConfigurationManager.GetSetting("FromAddress");
            EmailListFilterCamlQueryString = @"<ViewFields>
                                                  <FieldRef Name='Email' />
                                                  <FieldRef Name='FirstName' />
                                                  <FieldRef Name='LastName' />
                                                  <FieldRef Name='ID' />
                                               </ViewFields>                                               
                                               <Where>
                                                  <Or>
                                                     <Eq>
                                                        <FieldRef Name='Status' />
                                                        <Value Type='Choice'>Not Processed</Value>
                                                     </Eq>
                                                     <Eq>
                                                        <FieldRef Name='Status' />
                                                        <Value Type='Choice'>Error Occurred</Value>
                                                     </Eq>
                                                  </Or>
                                               </Where>";
            

            SharePointListName = CloudConfigurationManager.GetSetting("SharePointListName");
            SharePointSiteUrl = CloudConfigurationManager.GetSetting("SharePointSiteUrl");
            if (!SharePointSiteUrl.Contains("https:"))
                throw new ArgumentException("SharePointSiteUrl should be in https: format");

            SharePointUserName = CloudConfigurationManager.GetSetting("SharePointUserName");

            var plainPassword = CloudConfigurationManager.GetSetting("SharePointUserPassword");
            SecureString password = new SecureString();
            foreach (char c in plainPassword)
                password.AppendChar(c);

            SharePointUserPassword = password;
        }

        public string ExchangeUserName
        {
            get;
            private set;
        }

        public string ExchangeUserPassword
        {
            get;
            private set;
        }

        public string FromAddress
        {
            get;
            private set;
        }

        public string EmailListFilterCamlQueryString
        {
            get;
            private set;
        }

        public string SharePointListName
        {
            get;
            private set;
        }
        public string SharePointSiteUrl
        {
            get;
            private set;
        }

        public string SharePointUserName
        {
            get;
            private set;
        }

        public SecureString SharePointUserPassword
        {
            get;
            private set;
        }
    }
}
